'''
Author: your name
Date: 2020-11-09 17:05:02
LastEditTime: 2020-11-09 17:16:37
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \python-study\djangoS\app01\app01\Views\Forms\index.py
'''
from django.shortcuts import render, HttpResponse,redirect
from .My_forms import EmpForm
from Models.models import Emp
from django.core.exceptions import ValidationError
# Create your views here.

class addEmp():
    def add_emp(request):
        if request.method == "GET":
            form = EmpForm()  # 初始化form对象
            return render(request, "add_emp.html", {"form":form})
        else:
            form = EmpForm(request.POST)  # 将数据传给form对象
            if form.is_valid():  # 进行校验
                data = form.cleaned_data
                data.pop("r_salary")
                Emp.objects.create(**data)
                return redirect("/index/")
            else:  # 校验失败
                clear_errors = form.errors.get("__all__")  # 获取全局钩子错误信息
                return render(request, "add_emp.html", {"form": form, "clear_errors": clear_errors})